using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Microsoft.DirectX.DirectDraw;
using TowerDefense_Dx.Core;

namespace TowerDefense_Dx.GameLogic
{
    public class Enemy:DxSprite
    {
        public enum DIRECTION
        {
            LEFT = 0,
            RIGHT = 1,
            UP = 2,
            DOWN = 3
        }
        public enum ESTATE
        {
            NORMAL = 0,
            DEAD = 1,
            END = 2,
        }
        private int _health;
        private float _xSpeed;
        private float _ySpeed;
        private int _bountyScore;
        public int BountyScore
        {
            get { return _bountyScore; }
            set { _bountyScore = value; }
        }     
        private int _currentWayPointIndex = 0;       
        private DIRECTION _currentDirection ;
        public DIRECTION CurrentDirection
        {
            get { return _currentDirection; }
        }
        private ESTATE _enemyState = ESTATE.NORMAL;
        public ESTATE EnemyState
        {
            get { return _enemyState; }
            set { _enemyState = value; }
        }
        public Enemy(string imagePath, Device graphicDevice, int frameWidth, int frameHeight,PointF position):
            base(imagePath, graphicDevice, frameWidth, frameHeight)
        {
            _health = 100;
            _xSpeed = 0.5f;
            _ySpeed = 0.5f;
            _bountyScore = 2;
            _position = position;
            _center.X = _position.X + (_frameWidth / 2);
            _center.Y = _position.Y + (_frameHeight / 2);
        }
        public void Move(int[] wayPoint)
        {
            _currentDirection = GetDirection(wayPoint[_currentWayPointIndex],wayPoint[_currentWayPointIndex+1]);

            PointF nextWayPoint = GetLocation(wayPoint[_currentWayPointIndex + 1]);
            if ((int)this._position.X  == nextWayPoint.X  && (int)this._position.Y  == nextWayPoint.Y )
            {                
                _currentWayPointIndex++;
            }
            switch (_currentDirection)
            {
                case DIRECTION.LEFT:
                    {
                        this._position.X -= _xSpeed;
                        this._center.X = this._position.X + (_frameWidth / 2);
                        break;
                    }
                case DIRECTION.RIGHT:
                    {
                        this._position.X += _xSpeed;
                        this._center.X = this._position.X + (_frameWidth / 2);
                        break;
                    }
                case DIRECTION.UP:
                    {
                        this._position.Y -= _ySpeed;
                        this._center.Y = this._position.Y + (_frameHeight / 2);
                        break;
                    }
                case DIRECTION.DOWN:
                    {
                        this._position.Y += _ySpeed;
                        this._center.Y = this._position.Y + (_frameHeight / 2);
                        break;
                    }

            }
            PointF lastWayPoint = GetLocation(wayPoint[wayPoint.Length - 1]);
            if ((int)_position.X == lastWayPoint.X  && (int)_position.Y == lastWayPoint.Y )
            {
                _enemyState = ESTATE.END;
                
            }
        }
        public void GetHit(int damage)
        {           
            _health -= damage;
            if (_health <= 0)
            {
                _enemyState = ESTATE.DEAD;
            }            
        }
        private DIRECTION GetDirection(int currentWayPoint, int nextWayPoint)
        {
            int currentX = (currentWayPoint - 1) % 16 * 32;
            int currentY = (currentWayPoint - 1) / 16 * 32;
            int nextX = (nextWayPoint - 1) % 16 * 32;
            int nextY = (nextWayPoint - 1) / 16 * 32;
            DIRECTION direction = new DIRECTION();
            if (currentX < nextX)
            {
                direction = DIRECTION.RIGHT;
            }
            else if (currentX > nextX)
            {
                direction = DIRECTION.LEFT;
            }
            else if (currentY < nextX)
            {
                direction = DIRECTION.DOWN;
            }
            else if (currentY > nextY)
            {
                direction = DIRECTION.UP;
            }
            return direction;
        }
        private PointF GetLocation(int index)
        {
            PointF location = new PointF((index - 1) % 16 * 32 + 3, (index - 1) / 16 * 32 + 96);
            return location;
        }
    }
}
